package com.nageffer.shortlink.project.statsStrategy;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@RequiredArgsConstructor
public class UseMqFactory implements ApplicationContextAware {

    private static Map<String, statsRecord> typesMap = new ConcurrentHashMap<>();

    private final MqTypes mqTypes;

    @Value("${short-link.type}")
    private String mqType;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        mqTypes.getTypes().forEach((k,v) -> {
            typesMap.put(k, (statsRecord)applicationContext.getBean(v));
        });
    }

    public statsRecord getMqTypes() {
        statsRecord statsRecord = typesMap.get(mqType);
        return statsRecord;
    }

}
